package com.lsongh.springboot2.controller;

import com.lsongh.springboot2.entity.Result;
import com.lsongh.springboot2.entity.User;
import com.lsongh.springboot2.service.UserService;
import com.lsongh.springboot2.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    UserService userService;

    @GetMapping("/")
    public String hello() {
        return "hello";
    }

    @PostMapping("/login")
    public Result<User> login(@RequestParam("account") String account,
                              @RequestParam("password") String password) {
        User user = userService.findByAccount(account);
        if (user == null){
            return ResultUtil.error("该系统没有该账户！");
        }
        if (user.getPassword().equals(password)) {
            return ResultUtil.success(user);
        } else {
            return ResultUtil.error("密码不正确");
        }
    }
}
